package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.LauncherActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.List;

public  class RateListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private static final String TAG = "RateListActivity";
    private Handler handler;
    private ProgressBar bar;
    private  ArrayAdapter adapter;
    private GridView mylist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_list);


        mylist = findViewById(R.id.mylistview);
        bar = findViewById(R.id.progressBar);
        mylist.setEmptyView(bar);
        mylist.setOnItemClickListener(this);

//        String data[]  ={};
//        ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
//        mylist.setAdapter(adapter);

        handler = new Handler(Looper.myLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                //收到数据
                if (msg.what==5){
                    List<String> list = (List<String>) msg.obj;
                    //用于刷新列表
                    adapter = new ArrayAdapter<String>(RateListActivity.this,
                            android.R.layout.simple_list_item_1,
                            list);
                    mylist.setAdapter(adapter);
                    //屏蔽bar
                    //bar.setVisibility(View.INVISIBLE);
                }
                super.handleMessage(msg);
            }
        };
        //开启子线程
        MyTask myTask = new MyTask();
        myTask.setHandler(handler);
        Thread t = new Thread(myTask);
        t.start();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemClick: pos=" + position);
        //删除数据
        //adapter.remove(mylist.getItemAtPosition(position));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示").setMessage("请确认是否删除当前数据").setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG, "onClick: 对话框事件处理");
                adapter.remove(mylist.getItemAtPosition(position));
                //删除数据项
                //listItems.remove(position);
                //更新适配器
                //listItemAdapter.notifyDatasetChanged();
            }

        }).setNegativeButton("否",null);
        builder.create().show();

    }
}